package cn.lg.soar.general.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.general.api.service.IFileApi;
import cn.lg.soar.general.api.utils.BizFileBuilder;
import cn.lg.soar.general.config.GeneralCacheKey;
import cn.lg.soar.general.entity.WebPosition;
import cn.lg.soar.general.mapper.WebPositionMapper;
import cn.lg.soar.general.model.WebContentVo;
import cn.lg.soar.general.model.WebPositionVO;
import cn.lg.soar.general.service.IWebContentService;
import cn.lg.soar.general.service.IWebPositionService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * 页面位置
 * @author luguoxiang
 * @since 2023-03-19
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class WebPositionServiceImpl extends BaseServiceImpl<WebPositionMapper, WebPosition> implements IWebPositionService {

    @Autowired
    private IWebContentService contentService;
    @Autowired
    private IFileApi fileApi;

    @Cacheable(cacheNames = GeneralCacheKey.WEB_POSITION)
    @Override
    public WebPosition getByCode(String code) {
        return getOne(
                Wrappers.<WebPosition>lambdaQuery().eq(WebPosition::getCode, code)
        );
    }

    @Override
    public List<WebContentVo> contents(String code, Integer pageSize) {
        WebPosition position = getByCode(code);
        AssertUtil.notNull(position, "位置不存在");
        return contentService.voListByPositionId(position.getId(), pageSize);
    }

    @CacheEvict(cacheNames = GeneralCacheKey.WEB_POSITION, key = "#entity.code")
    @Override
    public boolean updateById(WebPosition entity) {
        return super.updateById(entity);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void remove(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        contentService.removeByPositionIds(ids);
        AssertUtil.isTrue(super.removeByIds(ids), "删除失败");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Long create(WebPositionVO data) {
        AssertUtil.isTrue(save(data), "操作失败");
        // 保存附件信息
        boolean saveFileFlag = BizFileBuilder.writer(data.getId())
                .mapper("WebPositionImg", data::getImgs)
                .execute(fileApi);
        AssertUtil.isTrue(saveFileFlag, "保存附件失败");
        return data.getId();
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(WebPositionVO data) {
        AssertUtil.isTrue(updateById(data), "操作失败");
        // 保存附件信息
        boolean saveFileFlag = BizFileBuilder.writer(data.getId())
                .mapper("WebPositionImg", data::getImgs)
                .execute(fileApi);
        AssertUtil.isTrue(saveFileFlag, "保存附件失败");
    }

    @Override
    public WebPositionVO detail(Long id) {
        WebPosition data = getById(id);
        WebPositionVO vo = WebPositionVO.from(data);
        // 获取附件信息
        BizFileBuilder.reader(id)
                .mapper("WebPositionImg", vo::setImgs)
                .execute(fileApi);
        return vo;
    }
}
